<?php
/**
 * 发起http post请求(REST API), 并获取REST请求的结果
 * @param string $url
 * @param string $param
 * @return - http response body if succeeds, else false.
 */
function request_post($url = '', $param = '')
{
 if (empty($url) || empty($param)) {
  return false;
 }

 $postUrl  = $url;
 $curlPost = $param;
 // 初始化curl
 $curl = curl_init();
 curl_setopt($curl, CURLOPT_URL, $postUrl);
 curl_setopt($curl, CURLOPT_HEADER, 0);
 // 要求结果为字符串且输出到屏幕上
 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
 // post提交方式
 curl_setopt($curl, CURLOPT_POST, 1);
 curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
 // 运行curl
 $data = curl_exec($curl);
 curl_close($curl);

 return $data;
}

$token = '24.a86d391293e89e2557c222038524d309.2592000.1592486604.282335-19958093';
$url   = 'https://aip.baidubce.com/rest/2.0/image-classify/v1/car?access_token=' . $token;
$img   = file_get_contents('/Users/lk/Desktop/car1.jpg');
$img   = base64_encode($img);
$bodys = array(
 'image' => $img,
// //  'top_num' => 5,
);
$res = request_post($url, $bodys);

print_r($res);
